package com.example.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
    private static String jdbcName; //用于描述驱动信息
    private static String dbUrl; //用于描述Url信息
    private static String dbUsername; //用于描述用户名信息
    private static String dbPassword; //用于描述密码信息

    //进行静态成员的初始化操作
    static{
        jdbcName="com.mysql.jdbc.Driver";
        dbUrl="jdbc:mysql://localhost:3306/db9";
        dbUsername="root";
        dbPassword="15061129384poi..";

        try {
            Class.forName(jdbcName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //获取连接
    public static Connection getConnection() throws SQLException {
        Connection connection= DriverManager.getConnection(dbUrl,dbUsername,dbPassword);
        return connection;
    }

    //关闭连接
    public static void closeConnection(Connection connection) throws SQLException {
         if(null!=connection){
             connection.close();
         }
    }

    public static void closeResource(Connection connection, Statement statement) throws SQLException {
        if(null!=connection){
            connection.close();
        }
        if(null!=statement){
            statement.close();
        }
    }
}
